package com.easy.common.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.easy.common.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
    @Autowired
    private IOrderService orderService;
    //appid
    private static final String APP_ID = "9021000154659391";
    //应用私钥
    private static final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCKs3bTslo3ngE1O0z+18xmHD4zJG1Y6sx0uWcmVHI2M4y9atNeP5WRbtLvaZOdrohlVLwpe2K8QnH8kYeIh3mPKip3qrAvNBsrWeygFcH5neJb/59bCaMV7z37lmB1drWygY8wwPBe9RkX16yd6vxDfnpdkii+Es7ePFDWRDat44IR/lNMzhkA3luAZNuy5eiutzFG3/M463EF/swDORkPuDoSdiyo6uRN/T+03EG+gkVaRRoV7j6bLVzc8xAOdMWuMtQNgW8jnKaJ2jkSpEMFOSqvhcLy1dcP0WBiylhV3WBpoI+RxgygCep6qtK8R24ZqCH/1ikIHjRbRFD1pN4XAgMBAAECggEAC+Tf579/pQ1TpTD0pVX0HT2Gx/PzvJBGACx1//9129gH1aZ3gbmKJfKO/CDdm8P/xlDB4fzGtsYr6DDCop6bef9vrNtiSGn7eng3pnK3M6SqaKE/FRt6dMMTWaPHmcsYQbqCgPfKpRn3kOfZ0aD5CQCrlaKXA9WxD7W0aOtolPixIf/rAiMtgnfBj+84UOXBZCGySow3c1qFKk6V9cp/Si5yTD0EqT0hdoETNaL6wTPQ9E+cnlY9MSTTzfquRzXzJQG8ZT8BXj5ZukXuEb7fsrTsQUl1HIHhi2Eh77EkLKOsDlxIwU/eshEI7swKnXiQMaak71HTzmrtcJmFzrttwQKBgQDLH021w0q3M6Pu1UpuXwKdOIH5BKqYRWqxQegz5+NfMVr8FSDnsMhNQpufTtTamyfkCItbt97prjtKnFWnXdhCydREo3tVgboXoR3J9U8z+MS9pLhTzUtXssWlg8YTNnQZUU9PKw2+jA9M5N88lTnpftr7n8SrQs0tMWDMcjXb/wKBgQCuzu35F+Yna7Cs0bIC6e58wPQ+cUkf75Y5znxA0BAS78bmaIBpTsijoiH1XYw2qbRKuEgvyxJEvMBJJqDb8LzWNTqaB0uVec/5eLHZuFjRwVuCzmoFvelVZgjBNfidaWTfgliUggW+C/livXjM7YNUZr5F/V5sFejP+gQF3kxd6QKBgGbIqV8fupAn7n1tquNc6oq6THLBHL4+6jVWP3T8k37ZD8xduK/lMgZ+fRdJIcaZGr92X2aDDaa8Br7Di0hM4aEyVlqBS2jCY3OPkO5gVrra2FuKnceyK7zt9rvBiZqxRGCHV5WdGLBiWV+0tQ4ICS0Rhygq+a8b94013mC2LnDrAoGADs5qK7IRhX9CnLC6QjLsR1bUmUFhLzpM4CKUNa0p32C92SA62DCbH/kIpU7CJJMlOTr4qkuqBXZzVv7Vmyt94yrpBsjdZxDFQ90sO0O5CjM1fBPduRTH4brdvPfEUhjuOVxCAnos4FcFTY6836ayfACj1yvJxp/oc0068ABPOZkCgYEAu/ibBWs3KQdVP70XRwJkc702MZd4TAOsDH36tU5+k0d3+DEX+aaxOmVcA6IQTEUmlsVDKO6i/DUpjS7jW7zV+ebpOC5HR5/+D00NGqItwv6THfXV2UD4cQMDBqogh89tbdqqT3EjbtdrNWEHWcpHW8iFjqcwkn2xg2bsvVm5Ty4=";

    private static final String CHARSET = "UTF-8";
    // 支付宝公钥
    private static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoBYADwR2XhJ7tq/msV3Tdwdbdpl2qIUtfMNvoBYoLnA3fb6CYXgTpNnJcfukm0PSnxdsp+FGOgFRzoFl63hLD2u/ndd4Q3UAaoVYe/J/Vn5QOsdjaKgu2BqOS7o1rC61UJOkEyPs3ijcObVzKqHfAS8LsTjrsZVTj3E81DvdFnQ6gabak+7LMKU9uZABRZxiOf+9p2TLrVyA1REt/9EeVtwk/WRJM3338ILTM/rUjinz5LB2tKf6XSN68gt9K3+v0UqXixiYHxWC752aCN7ZsXE7Dp3qFXbUspKRsgEMM2tLlvX5mkC19fYyL6eCHPsI3M9ZC/iCjc314pep5pjJoQIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private static final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private static final String FORMAT = "JSON";
    //签名方式
    private static final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private static final String NOTIFY_URL = "http://xht685.natappfree.cc/api/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private static final String RETURN_URL = "http://localhost:8081/#/user/orders";
    private static AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public static String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public static String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
